package com.qf.blogsystem.dao.impl;

import com.qf.blogsystem.core.utils.DruidUtils;
import com.qf.blogsystem.dao.DiscussPostDao;
import com.qf.blogsystem.entity.DiscussPost;
import com.qf.blogsystem.vo.DiscussPostVo;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Crazy
 * @version 1.0
 * @description: 帖子Dao实现类
 * @date 2022/12/6 10:09
 */
public class DiscussPostDaoImpl implements DiscussPostDao {
    /**
     * 创建DButils执行对象
     */
    private QueryRunner queryRunner=new QueryRunner(DruidUtils.getDataSource());
    /**
     * 添加帖子信息
     * @param discussPost
     * @return Integer
     * @throws Exception
     */
    @Override
    public Integer insert(DiscussPost discussPost) throws Exception {
        String sql = "insert into t_discuss_post(userId,title,content,type,status,createTime,commentCount,score) values(?,?,?,?,?,?,?,?)";
        return queryRunner.update(sql, discussPost.getUserId(), discussPost.getTitle(), discussPost.getContent(), discussPost.getType(), discussPost.getStatus(), discussPost.getCreateTime(), discussPost.getCommentCount(), discussPost.getScore());
    }

    /**
     * 根据帖子id查询帖子信息
     * @param id
     * @return DiscussPost
     * @throws Exception
     */
    @Override
    public DiscussPost selectById(Integer id) throws Exception {
        String sql = "select * from t_discuss_post d where id = ?";
        return queryRunner.query(sql, new BeanHandler<DiscussPost>(DiscussPost.class), id);
    }

    @Override
    public Integer updateById(DiscussPost discussPost) throws Exception {
        return null;
    }

    @Override
    public Integer deleteById(Integer id) throws Exception {
        return null;
    }

    @Override
    public List<DiscussPost> selectList() throws Exception {
        return null;
    }

    @Override
    public Integer batchDelete(List<Integer> ids) throws Exception {
        return null;
    }

    /**
     * 查询所有帖子信息
     * @return List<DiscussPost>
     * @throws Exception
     */
    @Override
    public List<DiscussPostVo> selectAllDiscussPost() throws Exception {
        String sql = "select d.id, d.userId, d.title, d.content, d.type, d. status, d.createTime, d.score, u.username, u.headerUrl from t_discuss_post d left join t_user u on (u.id = d.userId)";
        return queryRunner.query(sql,new BeanListHandler<DiscussPostVo>(DiscussPostVo.class));
    }
}
